package com.shiguo365.market.display.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.shiguo365.market.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserAddressAdapter extends RecyclerView.Adapter<UserAddressAdapter.ViewHolder> {

    private static final String TAG = "UserAddressAdapter";

    private final Activity activity;
    private final LayoutInflater inflater;
    private final List<JSONObject> addressList;

    public UserAddressAdapter(@NonNull Activity activity, @NonNull List<JSONObject> addressList) {
        this.activity = activity;
        inflater = LayoutInflater.from(activity);
        this.addressList = addressList;
    }

    @Override
    public int getItemCount() {
        return addressList.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.item_user_address, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.update(position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.user_address_item_tv_name)
        protected TextView tvName;

        @BindView(R.id.user_address_item_tv_phone)
        protected TextView tvPhone;

        @BindView(R.id.user_address_item_tv_address)
        protected TextView tvAddress;

        private JSONObject address;

        protected ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        protected void update(int position) {
            address = addressList.get(position);

            tvName.setText(address.getString("name"));
            tvPhone.setText(address.getString("phone"));
            tvAddress.setText(address.getString("address"));

        }

        @OnClick(R.id.user_address_item_btn_set_default)
        public void onBtnSetDefaultClick(View view){
            Log.d(TAG,"onBtnSetDefaultClick pressed~");
        }

    }

}
